/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Pengembalian.java
 *
 * Created on Jan 17, 2012, 10:06:57 PM
 */

package tugasbesarjeni1.form;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import tugasbesarjeni1.Database.VCD.penGembalian;
import tugasbesarjeni1.Database.query;
import tugasbesarjeni1.form.model.PengembalianModel;

/**
 *
 * @author Indy
 */
public class Pengembalian extends javax.swing.JPanel {
    private PengembalianModel pengembalianModel;
    private query Query = new query();

    /** Creates new form Pengembalian */
    public Pengembalian() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelPengembalian = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        tDenda = new javax.swing.JTextField();
        bSimpan = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        bCekSewa = new javax.swing.JButton();
        tNoID = new javax.swing.JTextField();
        tglSekarang = new javax.swing.JFormattedTextField();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "PENGEMBALIAN", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Rockwell", 1, 24))); // NOI18N
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());

        tabelPengembalian.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tabelPengembalian);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel2.setText("Denda Rp");

        tDenda.setEditable(false);
        tDenda.setFont(new java.awt.Font("Tahoma", 1, 24));
        tDenda.setText("0");
        tDenda.setBorder(null);
        tDenda.setOpaque(false);

        bSimpan.setText("Simpan");
        bSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSimpanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tDenda, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(144, 144, 144)
                        .addComponent(bSimpan)))
                .addContainerGap(314, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tDenda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bSimpan)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.add(jPanel4, java.awt.BorderLayout.PAGE_END);

        jPanel1.add(jPanel2, java.awt.BorderLayout.CENTER);

        jLabel1.setText("No. ID");

        bCekSewa.setText("Cek Sewa");
        bCekSewa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCekSewaActionPerformed(evt);
            }
        });

        tNoID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tNoIDActionPerformed(evt);
            }
        });

        tglSekarang.setBorder(null);
        tglSekarang.setEditable(false);
        tglSekarang.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tglSekarang.setOpaque(false);
        tglSekarang.setValue(new java.util.Date());

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tNoID)
                    .addComponent(bCekSewa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 265, Short.MAX_VALUE)
                .addComponent(tglSekarang, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tNoID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tglSekarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bCekSewa)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel3, java.awt.BorderLayout.PAGE_START);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 553, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tNoIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tNoIDActionPerformed
 if (tNoID.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Silakan mengisi no. ID", "Perhatian", JOptionPane.WARNING_MESSAGE);
        } else {
            cek();
        }
    }//GEN-LAST:event_tNoIDActionPerformed

    private void bSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSimpanActionPerformed
    simpan();
    }//GEN-LAST:event_bSimpanActionPerformed

    private void bCekSewaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCekSewaActionPerformed
    tNoIDActionPerformed(evt);
    }//GEN-LAST:event_bCekSewaActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCekSewa;
    private javax.swing.JButton bSimpan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField tDenda;
    private javax.swing.JTextField tNoID;
    private javax.swing.JTable tabelPengembalian;
    private javax.swing.JFormattedTextField tglSekarang;
    // End of variables declaration//GEN-END:variables
public void initForm(){
    pengembalianModel = new PengembalianModel();
        tabelPengembalian.setModel(pengembalianModel);
        tNoID.requestFocus();
}
private void cek() {
        try {                        
            int dendaSewa = 0;
            Date tgl_kembali = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date tanggal = new java.util.Date();
            tanggal = (Date) tglSekarang.getValue();
            String tgl_sekarang = dateFormat.format(tanggal);

            ResultSet resultSet = Query.selectPeminjaman(tNoID.getText());
            if (resultSet.next()) {
                String nama = resultSet.getString(1);
                clearTable();
                ResultSet rs = Query.searchPeminjaman(tNoID.getText(), nama);
                while (rs.next()) {
                    penGembalian p = new penGembalian();
                    p.setNoID(rs.getString(1));
                    p.setNama(rs.getString(2));
                    p.setKode(rs.getString(3));
                    p.setJudul(rs.getString(4));
                    p.setTglPinjam(rs.getString(5));
                    p.setTglKembali(rs.getString(6));
                    pengembalianModel.add(p);
                }
                int row = tabelPengembalian.getRowCount();
                if (row == 0) {
                    JOptionPane.showMessageDialog(null, "Anggota tidak melakukan peminjaman", "Perhatian", JOptionPane.WARNING_MESSAGE);
                    tNoID.setText("");
                    tNoID.requestFocus();
                }
                ResultSet set = Query.selectTanggal(tNoID.getText());
                if (set.next()) {
                    tgl_kembali = set.getDate(1);
                    ResultSet rs2 = Query.selectSelisihTanggal(tgl_sekarang, tgl_kembali);
                    if (rs2.next()) {
                        int dnd = rs2.getInt(1);
                        if (dnd <= 0) {
                            dendaSewa = 0;
                            tDenda.setText("" + dendaSewa);
                        } else {
                            dendaSewa = 1000 * dnd;
                            tDenda.setText("" + dendaSewa);
                        }
                    }
                }
            } else {
                JOptionPane.showMessageDialog(null, tNoID.getText() + " tidak terdaftar", "Perhatian", JOptionPane.WARNING_MESSAGE);
                tNoID.setText("");
                tNoID.requestFocus();
                clearTable();
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
  private void simpan() {
        if (tNoID.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Silakan isi no. id");
        } else {
            try {                
                ResultSet rs = Query.selectPeminjaman(tNoID.getText());
                while (rs.next()) {
                    String noID = rs.getString(1);
                    String kode = rs.getString(2);
                    String tglPinjam = rs.getString(3);
                    String tglKembali = rs.getString(4);
                    Query.insertPengembalian(noID, kode, tglPinjam, tglKembali);
                }                
                JOptionPane.showMessageDialog(null, "Transaksi berhasil disimpan");                
                Query.deletePeminjaman(tNoID.getText());
                clearTable();
                tNoID.setText("");
                tDenda.setText("0");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
  private void clearTable() {
        try {
            int row = tabelPengembalian.getRowCount();
            for (int i = 0; i < row; i++) {
                pengembalianModel.hapusTabel(0, row);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "" + e.getMessage());
        }
    }
}
